<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/5 0005
 * Time: 下午 4:10
 */
namespace app\common;

class Helper{
    /**
     * curl POST方式实现
     * @param $url
     * @param $postData
     * @return mixed
     * @author fei <xliang.fei@gmail.com>
     */
    public static function curlPost($url, $postData)
    {
        //初始化
        $curl = curl_init(); //用curl发送数据给api
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        $response = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        return $response;
    }

    /**
     * curl GET方式实现
     * @param $url
     * @return mixed
     * @author fei <xliang.fei@gmail.com>
     */
    public static function curlGet($url)
    {
        //初始化
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        return $data;
    }

    //生成唯一ID
    public static function getUUID($prefix=''){
        $uuid = md5(uniqid(mt_rand(), true));
        return $prefix . $uuid;
    }

    public static function getUUID22(){
        $uuid = md5(uniqid(mt_rand(), true));
        return substr($uuid,mt_rand(0,10),22);
    }

    //把其它地图的坐标转换成腾讯地图的坐标
    public static function CoordinateConvert($latlng){
        $url = sprintf(config('map.TencentMapUrl'),$latlng,config('map.type'),config('map.key'));
        $res = json_decode(self::curlGet($url),true);
        return $res['locations'][0]['lat'].','.$res['locations'][0]['lng'];
    }

    public static function GeocoderAddress($address){
        $url = 'http://apis.map.qq.com/ws/geocoder/v1/?address=' . $address . '&key=' . config('map.key');
        $res = json_decode(self::curlGet($url),true);
        return $res['result'];
    }

    /**
     * 后台登录密码加密
     *
     * @param $password 要加密的密码
     * @param string $algo 要使用的哈希算法，例如："md5"，"sha256"，"haval160,4" 等
     * @param string $key 加密秘钥
     * @return string
     */
    public static function passwordEncrypt($password, $algo = 'ripemd256', $key = 'mxkj@2018'){
        $password = hash($algo, $key . $password);

        return $password;
    }

    /**
     * 某个值是否存在多维数组中
     *
     * @param $value
     * @param $array
     * @return bool
     */
    public static function IsGetArray($value, $array){
        foreach($array as $item) {
            if(!is_array($item)) {
                if ($item == $value) {
                    return true;
                } else {
                    continue;
                }
            }

            if(in_array($value, $item)) {
                return true;
            } else if(self::IsGetArray($value, $item)) {
                return true;
            }
        }
        return false;
    }

    //通过经纬度计算距离
    public static function getDistance($lat,$lng,$store){
        //将角度转为狐度
        $radLat1 = deg2rad($lat);//deg2rad()函数将角度转换为弧度
        $radLat2 = deg2rad($store['lat']);
        $radLng1 = deg2rad($lng);
        $radLng2 = deg2rad($store['lng']);

        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6371.393 * 1000;
        return sprintf("%.2f",$s);
    }
}